package cn.tedu.ivos.vehicle.service.impl;

import cn.tedu.ivos.vehicle.mapper.VehicleMapper;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleSaveParam;
import cn.tedu.ivos.vehicle.pojo.entity.Vehicle;
import cn.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import cn.tedu.ivos.vehicle.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class VehicleServiceImpl implements VehicleService {

    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public List<VehicleVO> selectVehicle(VehicleQuery vehicleQuery) {
        List<VehicleVO> vehicleVOList = vehicleMapper.selectVehicle(vehicleQuery);
        return vehicleVOList;
    }

    @Override
    public void saveVehicle(VehicleSaveParam vehicleSaveParam) {
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleSaveParam,vehicle);
        if (vehicle.getId() == null){//新增
            vehicle.setCreateTime(new Date());
            vehicle.setStatus("1");
            vehicle.setGeofenceBindStatus("0");
            log.debug("新增车辆信息-saveVehicle:入参:{}",vehicle);
            vehicleMapper.insertVehicle(vehicle);
        }else{//修改
            vehicle.setUpdateTime(new Date());
            log.debug("修改车辆信息-saveVehicle:入参:{}",vehicle);
            vehicleMapper.updateVehicle(vehicle);
        }
    }

    @Override
    public Boolean deleteVehicle(Long vehicleId) {
        Boolean flag =vehicleMapper.deleteVehicle(vehicleId);
        return flag;
    }

    @Override
    public void unbind(Long vehicleId) {
        Vehicle vehicle = new Vehicle();
        vehicle.setId(vehicleId);
        vehicle.setGeofenceBindStatus("0");
        vehicle.setUpdateTime(new Date());
        vehicleMapper.unbind(vehicle);
    }

    @Override
    public void bind(Long geofenceId, Long id) {
        Vehicle vehicle = new Vehicle();
        vehicle.setId(id);
        vehicle.setGeofenceBindStatus("1");
        vehicle.setGeofenceId(geofenceId);
        vehicle.setUpdateTime(new Date());
        vehicleMapper.bind(vehicle);
    }
}
